﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Globalization;
using System.Collections;

namespace ResourceBlender_Express.Converters {
    /// <summary>
    /// Converts a list of language codes to a list of <code, englishName>
    /// </summary>
    public class LanguageCodeListConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            List<string> codes = value as List<string>;
            ObservableSortedDictionary<string, string> items = new ObservableSortedDictionary<string, string>(new Utils.DictionaryValueComparer(), new Utils.StringEqualityComparer());
            string englishName;
            foreach(string code in codes) {
                try {
                    englishName = CultureInfo.GetCultureInfo(code).EnglishName;
                } catch{
                    englishName = code;
                }
                items.Add(code, englishName);
            }
            return items;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// Converts a language code to it's english name if the code is supported by GetCultureInfo
    /// </summary>
    public class LanguageCodeConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            try {
                return CultureInfo.GetCultureInfo(value.ToString()).EnglishName;
            } catch {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }

    
    /// <summary>
    /// Converts a list of language codes to a string containing all codes, sorted by name
    /// </summary>
    public class LanguageCodeListStringifier : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            List<string> languageCodes = value as List<string>;
            int numLanguages = languageCodes.Count;
            string[] languageNames = new string[numLanguages];
            for(int i = 0; i < numLanguages; i++) {
                try {
                    languageNames[i] = CultureInfo.GetCultureInfo(languageCodes[i]).EnglishName;
                } catch {
                    languageNames[i] = languageCodes[i];
                }
            }
            Array.Sort(languageNames);
            StringBuilder buffer = new StringBuilder();
            for(int i = 0; i < numLanguages; i++) {
                buffer.Append(languageNames[i]);
                if(i < numLanguages-1) {
                    buffer.Append(", ");
                }
            }
            return buffer.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }

        /// <summary>
    /// Converts a dictionary of bundles to a string, sorted by bundle name
    /// </summary>
    public class BundleNameStringifier : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            string[] bundleNames = (value as IDictionary<string, string>).Values.ToArray();
            int numBundles = bundleNames.Length;
            //Array.Sort(languageNames);
            StringBuilder buffer = new StringBuilder();
            for(int i = 0; i < numBundles; i++) {
                buffer.Append(bundleNames[i]);
                if(i < numBundles - 1) {
                    buffer.Append(", ");
                }
            }
            return buffer.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
    

    /// <summary>
    /// Converts a ChangeableBundle.ChangeMode to an image which represnts the mode.
    /// </summary>
    public class ActionToImage : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            BitmapImage img = null;
            string imageName = string.Empty;
            if(value is KeyValuePair<string, ChangeableBundle>) {
                switch(((KeyValuePair<string, ChangeableBundle>)value).Value.Mode) {
                    case ChangeableBundle.ChangeMode.None:
                        imageName = "/images/bundle_16.png";
                        break;
                    case ChangeableBundle.ChangeMode.Add:
                        imageName = "/images/add_16.png";
                        break;
                    case ChangeableBundle.ChangeMode.Edit:
                        imageName = "/images/edit_16.png";
                        break;
                    case ChangeableBundle.ChangeMode.Delete:
                        imageName = "/images/delete_16.png";
                        break;
                }
            }
            try {
                img = new BitmapImage(new Uri(imageName, UriKind.Relative));
            } catch {
                img = new BitmapImage();
            }
            return img;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
}
